<?php
declare (strict_types=1);

namespace app\backend\controller\v3;

use app\common\model\BackendPermission as PermissionModle;
use think\db\Query;
use think\Request;
use app\backend\validate\PermissionValidate;

class BackendPermission extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $model = new PermissionModle();
        $datas = $model
            ->where('status', 1)
            ->select()
            ->toArray();
        $datas = getTree($datas);
        foreach ($datas as &$v) {
            if (!empty($v['children'])) {
                foreach ($v['children'] as &$vv) {
                    $vv['title'] = str_repeat(' &nbsp;', $vv['level'] * 2) . '|' . str_repeat('-', $vv['level'] * 2) . $vv['title'];
                }
            }
        }
        return sucessMsg(200, '获取成功！', $datas);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $model = new PermissionModle();
        $datas = $model->where(['status' => 1, 'pid' => 0])->field('id,title')->select();
        return sucessMsg(200, '获取成功！', $datas);
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(PermissionValidate::class)->scene('save')->check($postData);
            $model = new PermissionModle();
            if ($model->save($postData))
                return sucessMsg(200, '添加成功');
            return sucessMsg(422, '添加失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit()
    {
        //
        $model = new PermissionModle();
        $id = input('get.id/d');
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(PermissionValidate::class)->scene('update')->check($postData);
            $model = new PermissionModle();
            $cate = $model->where('id', $postData['id'])->find();
            if ($cate->save($postData))
                return sucessMsg(200, '修改成功');
            return sucessMsg(422, '修改失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @return \think\Response
     */
    public function delete()
    {
        //
        $model = new PermissionModle();
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if ($data->save(['id' => $id, 'status' => -1]))
            return sucessMsg(200, '删除成功');
        return errMsg(422, '删除失败');
    }
}
